//
//  TLPopMenu.m
//  Books
//
//  Created by tao故乡的云 on 2017/3/17.
//  Copyright © 2017年 故乡的云. All rights reserved.
//

#import "TLPopMenu.h"
#define kMagin 9 // 上边框
#define kMaginW 2 // 左右边框宽度

@interface TLPopMenu ()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic, weak) UIView *menu;
@end

@implementation TLPopMenu 

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        // 初始化设置
        [self setup];
    }
    return self;
}

- (void)setup {
    UIView *cover = [[UIView alloc] initWithFrame:self.frame];
    [self insertSubview:cover atIndex:0];
    [cover addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismiss)]];
    cover.backgroundColor = [UIColor colorWithRed:0.5 green:0.5 blue:0.5 alpha:0.2];
    
    UIView *menu = [[UIView alloc] init];
    [self addSubview:menu];
    _menu = menu;
    
    // 背景
    UIImageView *bg = [[UIImageView alloc] init];
    bg.image = [UIImage imageNamed:@"popover_background_right"];
    [menu addSubview:bg];
    
    // 子元素：itemsView
    UITableView *tableView = [[UITableView alloc] init];
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.backgroundColor = [UIColor clearColor];
    [menu addSubview:tableView];
    tableView.separatorInset = UIEdgeInsetsMake(0, 16, 0, 16);
}

- (void)dismiss {
    if ([self.delegate respondsToSelector:@selector(popMenuDismiss:)]) {
        [self.delegate popMenuDismiss:self];
    }
}

- (void)setItems:(NSArray<TLPopMenuItem *> *)items {
    _items = items;
    [self layoutIfNeeded];
}

- (void)layoutSubviews {
    [super layoutSubviews];
    CGRect frame = CGRectMake(self.bounds.size.width - 135, 0, 130, self.items.count * 30 + kMagin);
    self.menu.frame = frame;
    
    for (UIView *view in self.menu.subviews) {
        if ([view isKindOfClass:[UITableView class]]) {
            CGFloat w = self.menu.frame.size.width;
            CGFloat h = self.menu.frame.size.height - kMagin;
            CGRect frame = CGRectMake(kMaginW, kMagin, w - kMaginW * 2, h + kMaginW);
            view.frame = frame;
        }else {
            view.frame = self.menu.bounds;
        }
    }
}

#pragma mark - UITableViewDelegate, UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.items.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"popMenuCell"];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"popMenuCell"];
        cell.backgroundColor = [UIColor clearColor];
        
    }
    TLPopMenuItem *item = self.items[indexPath.row];
    cell.imageView.image = item.icon;
    cell.textLabel.text = item.title;
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"item---- %li",indexPath.row);
    if ([self.delegate respondsToSelector:@selector(popMenu:didSelectItemAtIndex:)]) {
        [self.delegate popMenu:self didSelectItemAtIndex:indexPath.row];
    }
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    cell.selected = NO;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 30;
}
@end
